﻿namespace EyeOpen.Data.EntityFramework.Test
{
	using System;
	using System.Data.Entity;
	using System.Linq;
	using EyeOpen.Data.EntityFramework;
	using EyeOpen.Domain;
	using Microsoft.VisualStudio.TestTools.UnitTesting;

	[TestClass]
	public class FinancialDbContextTest
	{
		public FinancialDbContextTest()
		{
			Database.SetInitializer(new CreateDatabaseIfNotExists<FinancialDbContext>());
		}

		[TestMethod]
		public void AddAndRetrieveCustomer()
		{
			var id = Guid.NewGuid();

			using (var context = CreateContext())
			{
				context.CustomerSet.Add(new Customer { Id = id, Name = "Bill" });
				context.SaveChanges();
			}

			using (var context = CreateContext())
			{
				var customer = context.CustomerSet.Single(c => c.Id == id);
				Assert.AreEqual("Bill", customer.Name);
			}
		}

		[TestMethod]
		public void AddCustomerAndOrder()
		{
			var id = Guid.NewGuid();

			using (var context = CreateContext())
			{
				var customer = new Customer { Id = id, Name = "Bill" };
				context.CustomerSet.Add(customer);

				var order = new Order { Id = Guid.NewGuid(), Name = "Banana", Price = 100, Customer = customer };
				context.OrderSet.Add(order);
				
				context.SaveChanges();
			}

			using (var context = CreateContext())
			{
				var customer = context.CustomerSet.Include(c => c.OrderList).Single(c => c.Id == id);
				Assert.AreEqual(1, customer.OrderList.Count());
			}
		}

		private FinancialDbContext CreateContext()
		{
			const string SqlServerConnection = "FinancialConnection";
			const string SqlCeConnection = "FinancialConnection.SqlCe";
			return new FinancialDbContext(SqlCeConnection);
		}
	}
}
